// pages/canvas/canvas.js
import { URL, ajax, Toast } from '../../utils/index.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    modelList: [],
    timeList: [
      { text: '近一个月', value: '1' },
      { text: '近三个月', value: '3' },
      { text: '近六个月', value: '6' },
      { text: '近十二个月', value: '12' }
    ],
    model: '',
    time: '1',
    radarData: null,
    // radarData: {
    //   list: [
    //     { name: '品牌介绍场景', max: 100 },
    //     { name: '样板间场景', max: 100 },
    //     { name: '购买场景', max: 100 },
    //     { name: '区域对比场景', max: 100 },
    //     { name: '户型场景', max: 100 },
    //     { name: '价值点场景', max: 100 }
    //   ],
    //   data: [60, 60, 60, 60, 60, 70]
    // },
    link: null
  },
  modelChange (e) {
    this.setData({
      model: e.detail
    })
    this.queryPracticeTrendDataRequest()
  },
  timeChange(e) {
    this.setData({
      time: e.detail
    })
    this.queryPracticeTrendDataRequest()
  },
  queryModelListRequest() {
    ajax.loading()
    return new Promise((resolve, reject) => {
      ajax.post('/applet/trend/queryModelList').then(res => {
        if (res.data.code == 1) {
          let arr = [{
            text: '全部',
            value: ''
          }]
          res.data.data.modelList.forEach(item => {
            arr.push({
              text: item.name,
              value: item.modelId
            })
          })
          this.setData({
            modelList: arr,
          }),
            ajax.toast.clear(), resolve(res)
        } else ajax.toast.fail(res.data.msg), reject(res)
      }).catch(e => (ajax.toast.fail('接口错误onload'), reject(e), console.log(e)))
    })
  },
  queryPracticeTrendDataRequest() {
    ajax.loading()
    return new Promise((resolve, reject) => {
      ajax.post('/applet/trend/queryPracticeTrendData', {
        month: this.data.time - 0,
        modelId: this.data.model - 0 || null
      }).then(res => {
        if (res.data.code == 1) {
          let {radarData} = this.data,{radar,time,link} = res.data.data,entity = [],data = [];
          let links = {
            xAxis: time,
            series: link,
            legend: []
          }
          links.series.forEach(item => links.legend.push(item.name))
          radar.forEach(item => {
            entity.push({
              name: item.name,
              max: 100
            })
            data.push(item.value)
          })
          radarData.list = entity
          radarData.data = data
          console.log(links)
          this.setData({
            radarData: radarData,
            link: links
          })
          console.log(this.data.radarData)
          ajax.toast.clear(), resolve(res)
        } else ajax.toast.fail(res.data.msg), reject(res)
      }).catch(e => (ajax.toast.fail('接口错误onload'), reject(e), console.log(e)))
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.homeRequest()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})